﻿using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine;

[JsonObject(MemberSerialization.OptIn)]
public abstract class Room:MonoBehaviour
{
    public BaseRoom BaseRoom { get; private set; }
    public Room()
    {
        string id = System.Guid.NewGuid().ToString("N");
        BaseRoom = new BaseRoom() {
            Id = id,
            Name = id
        };
        
    }
    /// <summary>
    /// 当前房间中的所有成员字典
    /// </summary>
    private Dict<string, Member> memDict = new Dict<string, Member>();

    [JsonProperty]
    /// <summary>
    /// 房间唯一Id
    /// </summary>
    public string Id { get { return BaseRoom.Id; } }
    [JsonProperty]
    /// <summary>
    /// 房间名称
    /// </summary>
    public string Name { get { return BaseRoom.Name; } }
    [JsonProperty]
    /// <summary>
    /// 房间中成员个数
    /// </summary>
    public int MemberCount { get { return memDict.Count; } }
    /// <summary>
    /// 获取房间中的所有成员
    /// </summary>
    public Member[] Members
    {
        get
        {
            return new List<Member>(memDict.Dictionary.Values).ToArray();
        }
    }
    /// <summary>
    /// 向当前房间中添加一个成员
    /// </summary>
    /// <param name="clientId">欲添加的成员Id</param>
    public virtual void Add(string clientId)
    {
        if(Singleton<MemberManager>.GetInstance().IsExist(clientId))
        {
            Member member = Singleton<MemberManager>.GetInstance().GetValue(clientId);
            if (!memDict.IsExist(clientId))
            {
                if(member.Room == null)
                    member.Room = this;
                else
                {
                    member.Room.Remove(clientId);
                    member.Room = this;
                }
                memDict.Add(clientId, member);
            }
        }
    }
    /// <summary>
    /// 在当前房间中添加一个成员
    /// </summary>
    /// <param name="mem">欲被添加的成员</param>
    public virtual void Add(Member mem)
    {
        Add(mem.Id);
    }
    /// <summary>
    /// 从当前房间中移除一个成员
    /// </summary>
    /// <param name="clientId">成员唯一Id</param>
    public virtual void Remove(string clientId)
    {
        if (memDict.IsExist(clientId))
        {
            memDict.GetValue(clientId).Room = null;
            memDict.Remove(clientId);
        }
    }
    /// <summary>
    /// 从当前房间中移除一个成员
    /// </summary>
    /// <param name="mem">成员实例</param>
    public virtual void Remove(Member mem)
    {
        Remove(mem.Id);
    }
    /// <summary>
    /// 获取房间中的一个成员
    /// </summary>
    /// <param name="clientId">成员唯一Id</param>
    /// <returns></returns>
    public virtual Member GetMember(string clientId)
    {
        return memDict.GetValue(clientId);
    }
    /// <summary>
    /// 发送消息给房间内的指定成员
    /// </summary>
    /// <param name="clientId">当前房间内的成员Id</param>
    /// <param name="msg">欲发送的消息</param>
    public virtual void Send(string clientId,string msg)
    {
        if (memDict.IsExist(clientId))
            memDict.GetValue(clientId).Send(msg);
    }
    /// <summary>
    /// 向当前房间成员发送广播消息
    /// </summary>
    /// <param name="msg">欲发送的消息</param>
    public virtual void SendToAll(string msg)
    {
        foreach (var item in memDict.Dictionary)
            Send(item.Key, msg);
    }
    public override string ToString()
    {
        return JsonConvert.SerializeObject(this);
    }
    /// <summary>
    /// 销毁当前房间所挂在的对象
    /// </summary>
    public void DestroySelf()
    {
        Destroy(gameObject);
    }
    public void ChangeName(string roomName)
    {
        BaseRoom.Name = roomName;
    }
}
